package com.wuseguang.springcloud.JdbcTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wuseguang.springcloud.util.WeChat.WXUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public class WXJdbc extends  BaseTemplate {

    private static Logger logger = LoggerFactory.getLogger(WXJdbc.class);

    //微信appId
    private static String AppId;
    //微信密钥
    private static String AppSecret;
    //开发者id
    private static Integer id;

    /**
     * 获取自己公司小程序的AccessToken
     * @param type 版本类型：(0校园版,1家长版)
     * @return
     */
    public  String getAccessToken(Integer type){

        //获取的token
        String token = null;
        Map<String,Object> tokenMap =null;

        //判断是那个版本
        if(type == 0){
            logger.info("这里是校园版，就是教师版");
            AppId = "wxeef24208ca48a4c2";
            AppSecret = "eb52a0824c03d0421ef996c67af702c9";
            id = 1;
            //获取开发者的token
             tokenMap = getAppletAccessToken(id);
        }else{
            logger.info("这里是家长版，就是学生版");
            AppId = "wx30b606bac7775eba";
            AppSecret = "a28468bfc8c9c7ed4277118f58beb85d";
            id = 2;
            //获取开发者的token
             tokenMap = getAppletAccessToken(id);
        }

        //判断如果为空就去微信获取token
        if(tokenMap == null || tokenMap.size()==0) {
            token = WXUtil.getToken(AppId, AppSecret);
            // 解析相应内容（转换成json对象）
            JSONObject json = JSON.parseObject(token);
            //token
            String accessTokeen = (String) json.get("access_token");
            //有效时间
            Integer expiresIn = (Integer) json.get("expires_in");
            insertAccessToken(id, String.valueOf(System.currentTimeMillis()+(expiresIn * 1000)), accessTokeen);
            //返回有效的token
            return accessTokeen;
            //过期
        }if(Long.valueOf(tokenMap.get("expires_in").toString())<System.currentTimeMillis()){
            token = WXUtil.getToken(AppId, AppSecret);
            // 解析相应内容（转换成json对象）
            JSONObject json = JSON.parseObject(token);
            //token
            String accessTokeen = (String) json.get("access_token");
            //有效时间
            Integer expiresIn = (Integer) json.get("expires_in");
            //修改掉过期的token
            updateAccessToken(Integer.valueOf(tokenMap.get("id").toString()), String.valueOf(System.currentTimeMillis()+(expiresIn * 1000)), accessTokeen);
            //返回有效的token
            return accessTokeen;
        }

        //返回未过期的token
        return tokenMap.get("access_token").toString();
    }

    /**
     * 获取公众号AccessToken
     * @param schoolId 学校id
     * @return
     */
    public  String  getOfficialAccountAccessToken(Integer schoolId){

        //获取的token
        String token = null;
        Map<String,Object> tokenMap =null;

        //判断是那个版本
        if(schoolId == 0){
            logger.info("这里是自己公司的");
            AppId = "wx488da3b3817ff049";
            AppSecret = "6385ae888eeeb70abf0bd1ba8760f1fc";
            id = 3;
            //获取开发者的token
            tokenMap = getAppletAccessToken(id);
        }else{
            logger.info("定制学校");
            //查询这个学校的公众号
            String sql = "select * from we_chat_developer where shcool_id = ? and type = 1";
            Map<String,Object> map = queryMap(sql,schoolId);
            AppId = map.get("app_id").toString();
            AppSecret = map.get("app_secret").toString();
            id = Integer.valueOf( map.get("id").toString());
            //获取开发者的token
            tokenMap = getAppletAccessToken(id);
        }

        //判断如果为空就去微信获取token
        if(tokenMap == null || tokenMap.size()==0) {
            token = WXUtil.getToken(AppId, AppSecret);
            // 解析相应内容（转换成json对象）
            JSONObject json = JSON.parseObject(token);
            //token
            String accessTokeen = (String) json.get("access_token");
            //有效时间
            Integer expiresIn = (Integer) json.get("expires_in");
            insertAccessToken(id, String.valueOf(System.currentTimeMillis()+(expiresIn * 1000)), accessTokeen);
            //返回有效的token
            return accessTokeen;
            //过期
        }if(Long.valueOf(tokenMap.get("expires_in").toString())<System.currentTimeMillis()){
            token = WXUtil.getToken(AppId, AppSecret);
            // 解析相应内容（转换成json对象）
            JSONObject json = JSON.parseObject(token);
            //token
            String accessTokeen = (String) json.get("access_token");
            //有效时间
            Integer expiresIn = (Integer) json.get("expires_in");
            //修改掉过期的token
            updateAccessToken(Integer.valueOf(tokenMap.get("id").toString()), String.valueOf(System.currentTimeMillis()+(expiresIn * 1000)), accessTokeen);
            //返回有效的token
            return accessTokeen;
        }

        //返回未过期的token
        return tokenMap.get("access_token").toString();
    }


}
